//
//  DHHeadTableViewCell.m
//  Moment
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "DHHeadTableViewCell.h"

@implementation DHHeadTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return  self;
}
-(void)createViews{
    _myScrollView = [[UIScrollView alloc]init];
    [self.contentView addSubview:_myScrollView];
[_myScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.and.bottom.and.left.and.right.equalTo(@0);
}];
}
-(void)setPicArray:(NSMutableArray *)picArray{
    if (_picArray != picArray) {
        _picArray = picArray;
    }
    [_myScrollView removeAllSubviews];
    [_pageC removeFromSuperview];
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:_picArray];
    [tempArr addObject:_picArray[0]];
    [tempArr insertObject:[_picArray lastObject] atIndex:0];
    self.myScrollView.showsHorizontalScrollIndicator = NO;//水平方向滚动条
    self.myScrollView.userInteractionEnabled = YES;//用户交互
    self.myScrollView.pagingEnabled = YES;//按页滑动
    self.myScrollView.contentSize = CGSizeMake(375*WIDTH*(self.picArray.count + 2), 180*HEIGHT);
    self.myScrollView.contentOffset = CGPointMake(375*WIDTH, 0);
    _myScrollView.bounces = NO;
    _myScrollView.delegate = self;
        for (int i = 0; i < tempArr.count; i++) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

        UIView *aview = [[UIView alloc]initWithFrame:CGRectMake(375 * i *WIDTH, 0, 375 * WIDTH, 180 *WIDTH)];
        [_myScrollView addSubview:aview];
        UIImageView *headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 180 * HEIGHT)];
            headImageView.userInteractionEnabled = YES;
        headImageView.tag = 1000 + i;
        DHCarousel *carousel = [[DHCarousel alloc]init];
        carousel = tempArr[i];
        NSURL *url = [NSURL URLWithString:carousel.img];
        [headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zanwei"]];
        [aview addSubview:headImageView];
        [headImageView addGestureRecognizer:tap];
    }
    
    self.pageC = [[UIPageControl alloc]init];
    [self.contentView addSubview:self.pageC];
    [_pageC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-15);
        make.top.equalTo(@145);
        make.bottom.equalTo(@-10);
        make.width.equalTo(@40);
        make.height.equalTo(@20);
    }];
    //    设置页码个数
    self.pageC.numberOfPages = self.picArray.count;
    self.pageC.currentPage = 0;

}
//轮播图滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_myScrollView.contentOffset.x == 375 * (self.picArray.count + 1)*WIDTH)
    {
        _myScrollView.contentOffset = CGPointMake(375*WIDTH, 0);
    }
    else if (self.myScrollView.contentOffset.x == 0)
    {
        self.myScrollView.contentOffset = CGPointMake(375 *(self.picArray.count)*WIDTH, 0);
    }
    // 图和点同步
    self.pageC.currentPage = (self.myScrollView.contentOffset.x / (375*WIDTH)) - 1;
}
-(void)tapAction:(UITapGestureRecognizer *)tap{
    DHCarousel *carousel =[[DHCarousel alloc]init];
    NSLog(@"tap.view.tag == %ld",tap.view.tag);
    carousel = _picArray[tap.view.tag - 1001];
    [self.deleage pushToRadioVC:carousel];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
